﻿//#define debug
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_menu : System.Web.UI.Page
{    
    public ConfigTable ThisTable = null;
    public string listType = string.Empty;
    public string listValue = string.Empty;
    public string map = string.Empty;
    public bool refresh = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConfigGlobal.CheckLog();
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();
        ThisTable = cTab;
        //添加grid字段，必须重新添加，因为模板列不添加的话不显示内容
        SetupGrid(cTab);
        
        if (!Page.IsPostBack)
        {
            BindGrid(cTab.GetListBaseSql(), cTab.GetListOrderSql());
            ViewState["LastSql"] = cTab.GetListBaseSql() + cTab.GetListOrderSql();

            
        }
        //map = GetMap();        
    }

    public string GetMap()
    {
        string r = string.Empty;
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                if (!string.IsNullOrEmpty(r))
                {
                    r += ",";
                }
                r += row.Cells[0].Text;
            }
        }
        this.GridView1.Columns[0].Visible = false;
        return r;
    }

    public override string StyleSheetTheme
    {
        get
        {
            //return base.StyleSheetTheme;
            return ConfigGlobal.GetGlobal().Theme;
        }
        set
        {
            base.StyleSheetTheme = value;
        }
    }

    #region 设置GridView
    private void SetupGrid(ConfigTable cTab)
    {
        //设置基本字段        
        cTab.SetupMenuGridFields(this.GridView1);        
        cTab.SetOrderLink(this.GridView1);
        cTab.SetGridAddChildLink(this.GridView1);
        //设置查看
        cTab.SetGridShowLink(this.GridView1);
        //设置编辑
        cTab.SetGridEditLink(this.GridView1);
        //设置删除
        cTab.SetGridDelLink(this.GridView1);
        //设置分页
        this.GridView1.AllowPaging = cTab.AllowPageing;
        this.GridView1.PageSize = cTab.PageSize;
    }
    #endregion

    #region 绑定Grid
    private void BindGrid(string listsql,string ordersql)
    {
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();        
        this.GridView1.DataSource = cTab.GetMenuDataSource(listsql,ordersql);
        this.GridView1.DataBind();
    }
    private void BindGrid(string sql)
    {
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();
        this.GridView1.DataSource = cTab.GetMenuDataSource(sql);
        this.GridView1.DataBind();
    }
    #endregion

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid(ViewState["LastSql"].ToString());
        e.Cancel = true;
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql = UIHelper.GridView1_Sorting(ViewState, sender, e);

        ConfigTable cTab = ThisTable;

        //绑定Grid
        BindGrid(cTab.GetListBaseSql(), sql);
        ViewState["LastSql"] = cTab.GetListBaseSql() + sql;

        #if debug
        Response.Write(ViewState["LastSql"].ToString());
        #endif
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        map = GetMap();
    }
}
